# encoding: utf-8


class GroupsController < ApplicationController

	before_filter :load_sidebar
  before_filter :require_login, :except => [:index, :show]
  before_filter :require_moderator, :only => [:new, :edit, :create, :update, :destroy, :add_members]
  before_filter :do_index, :only => [:index, :show]
  
  # Popis grupa
  def index
    @groups = Group.all
    @sections = GroupsSection.all
    @meta_title = "Grupe - Udruga dizajnera i programera"
    @meta_description = "Priključite se grupama vezanim uz tematike koje vas zanimaju i sudjelujte u raspravama.  "
  end

  # Pojedina grupa
  def show
    @group = Group.find(params[:id])

		# ako je unesen nestandardni URL
		if @group.url != request.path then
			redirect_to @group.url
		end 
		
    # prekini izvodjenje ako nema dopustenja (inace dolazi do double render errora)
    return unless require_group_permission(@group)

    @threads = @group.groups_threads.order("sticky DESC, updated_at DESC").all
    @meta_title = @group.name + " - UDIP Grupe"
    @meta_description = @group.description
  end
  
  # Oznaci sve grupe kao procitane
  def mark_all_as_visited
    @group = Group.find(params[:id])
    return unless require_group_permission(@group)

    @group.groups_threads.each do |thread|
      thread.visit current_member
    end

    redirect_to @group
  end

	# Dodaj korisnike
  def add_members
    @group = Group.find(params[:id])
    @member = Member.new
    @members = Member.where("permission_id <= ?",@group.permission.id) - @group.members
  end

  def save_members
    @group = Group.find(params[:id])
    @threads = @group.groups_threads.order("sticky DESC, updated_at DESC").all

    member_params = params[:member]
    member = Member.find(member_params[:id])

    unless member.is_in_group? params[:id]
      group_association = GroupAssociation.new
      group_association.member_id = member.id
      group_association.group_id = params[:id]
      if group_association.save then
      feed = Feed.new
      feed.content = @group
      feed.member = member
      feed.group = @group
      feed.save
      end

    end

    render :action => :show
  end

  # Pridruživanje grupi
  def join

    @group = Group.find(params[:id])
    @threads = @group.groups_threads.order("sticky DESC, updated_at DESC").all

    require_group_permission(@group)

    unless current_member.is_in_group? params[:id]
      group_association = GroupAssociation.new
      group_association.member_id = current_member.id
      group_association.group_id = params[:id]
      group_association.save
      
      feed = Feed.new
      feed.content = @group
      feed.member = current_member
      feed.group = @group
      feed.save
    end

    render :action => :show
  end

  # Napuštanje grupe
  def leave

    if current_member.is_in_group? params[:id]
      group_association = GroupAssociation.where( :group_id => params[:id], :member_id => current_member.id)
    group_association.first.destroy
    end

    @group = Group.find(params[:id])
    @threads = @group.groups_threads.order("sticky DESC, updated_at DESC").all
    render :action => :show
  end

  # Obrazac za stvaranje grupe
  def new
    @group = Group.new
    @group[:permission_id] = 4
    @permissions = Permission.all
    @sections = GroupsSection.all
  end

  # Obrazac za uređivanje grupe
  def edit
    @group = Group.find(params[:id])
    @permissions = Permission.all
    @sections = GroupsSection.all
  end

  # Stvaranje grupe
  def create
    @group = Group.new(params[:group])

    if @group.save
      redirect_to(@group, :notice => 'Grupa je stvorena.')
    else
      render :action => "new"
    end
  end

  # Uredi grupu
  def update
    @group = Group.find(params[:id])

    if @group.update_attributes(params[:group])
      redirect_to(@group, :notice => 'Group was successfully updated.')
    else
      render :action => "edit"
    end
  end

  # Brisanje
  def destroy
    @group = Group.find(params[:id])
    @group.destroy

    redirect_to(groups_url)
  end
end
